﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using JM.JCY.BusinessEntity;
using JM.JCY.Proxy;
using JM.JCY.Common;
using NCS.IConnect.ApplicationContexts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;


namespace JM.JCY.SharePointComponent.UserManagement.LeftMenu
{
    public partial class LeftMenuUserControl : UserControl
    {
        public string appName = JCYConstant.ApplicationName.StaffDirectory;

        protected void Page_Load(object sender, EventArgs e)
        {
         
            string sourceDirectory = this.Parent.AppRelativeTemplateSourceDirectory;    //Example:   ~/_CONTROLTEMPLATES/NCS.OCP.SharePointComponent.Broadcast/CreateBroadcastsRole/
            string webPartName = sourceDirectory.Substring(sourceDirectory.TrimEnd('/').LastIndexOf('/') + 1).TrimEnd('/');

            switch (webPartName.ToLower())
            {
                case "createuser":
                case "edituser":
                case "userlist":
                    lbUserMgr.CssClass = "leftmenuSelected";
                    break;

                case "createdept":
                case "editdept":
                case "deptlist":
                case "deptmgr":
                case "MemberSortOrder":
                    lbDeptMgr.CssClass = "leftmenuSelected";
                    break;
                    
                case "createrole":
                case "editrole":
                case "rolmgr":
                case "RoleMgrDetails":
                    lbRoleMgr.CssClass = "leftmenuSelected";
                    break;

                case "userpasswordreset":
                    lbResetPassword.CssClass = "leftmenuSelected";
                    break;
             }



 
        
            //if (!IsPostBack)
            //    SetLeftMenuVisibility(webPartName);
        }

        //protected void SetLeftMenuVisibility(string webPartName)
        //{
        //    bool isAppAdmin = false;
        //    bool isCPWithRole = false;
        //    bool hasCreateRes = false;
        //    bool hasEditRes = false;
        //    string StaffId = ApplicationContext.Current.GetKeyValue("StaffId").ToString().Trim();
        //    RoleManagementDataSet.T_OcpApplicationAdministratorDataTable appDT = new RoleManagementDataSet.T_OcpApplicationAdministratorDataTable();
        //    RoleManagementDataSet.T_OcpContentProviderDataTable cpDT = new RoleManagementDataSet.T_OcpContentProviderDataTable();
        //    using (RoleManagementProxy proxy = new RoleManagementProxy())
        //    {
        //        ////Check whether login staff is application administrator
        //        //if (Session["T_OcpApplicationAdministratorDataTableForStaffProfile"] == null)
        //        //{
        //        appDT = proxy.GetApplicationAdministratorByAppId(Convert.ToInt32(Common.OCPUtility.GetApplicationIdByName(appName)));
        //        //    Session["T_OcpApplicationAdministratorDataTableForStaffProfile"] = appDT;
        //        //}
        //        //else
        //        //    appDT = (RoleManagementDataSet.T_OcpApplicationAdministratorDataTable)Session["T_OcpApplicationAdministratorDataTableForStaffProfile"];

        //        foreach (RoleManagementDataSet.T_OcpApplicationAdministratorRow appRow in appDT.Rows)
        //        {
        //            if (appRow["StaffId"].ToString() == StaffId)
        //            {
        //                isAppAdmin = true;
        //                break;
        //            }
        //        }

        //        if (isAppAdmin == true)    //Login staff is application administrator, display Content Provider and Application Administrator menu
        //        {
        //            tbAppAdmin.Visible = true;
        //            tbCP.Visible = false;
        //        }

        //        //Check whether login user is content provider
        //        cpDT = proxy.SearchContentProviders(Convert.ToInt32(Common.OCPUtility.GetApplicationIdByName(appName)), null, null);
        //        foreach (RoleManagementDataSet.T_OcpContentProviderRow cpRow in cpDT.Rows)
        //        {
        //            if (cpRow["StaffId"].ToString() == StaffId)    //Login user is content provider
        //            {
        //                tbCP.Visible = true;
        //                string roleId = cpRow["RoleId"].ToString();
        //                RoleManagementDataSet.T_OcpRoleDataTable roleDT = new RoleManagementDataSet.T_OcpRoleDataTable();
        //                roleDT = proxy.SearchRoleByApplicationId(Convert.ToInt32(Common.OCPUtility.GetApplicationIdByName(appName)));
        //                foreach (RoleManagementDataSet.T_OcpRoleRow roleRow in roleDT.Rows)
        //                {
        //                    if (roleRow["RoleId"].ToString() == roleId)
        //                    {
        //                        string responsibilities = roleRow["Responsibility"].ToString();

        //                        if (responsibilities.IndexOf(OCPConstant.StaffProfile.Responsibility.ViewOrganisationStructure) >= 0)    //If user has View Organization Structure responsibility,then display View Organization Structure link
        //                            lbViewOS.Visible = true;
        //                        else
        //                            lbViewOS.Visible = false;

        //                        if (responsibilities.IndexOf(OCPConstant.StaffProfile.Responsibility.ViewOrganisationUnit) >= 0)    //If user has View Organization Structure responsibility,then display View Organization Structure link
        //                            lbViewOU.Visible = true;
        //                        else
        //                            lbViewOU.Visible = false;

        //                        if (responsibilities.IndexOf(OCPConstant.StaffProfile.Responsibility.CreateStaffProfile) >= 0
        //                            || responsibilities.IndexOf(OCPConstant.StaffProfile.Responsibility.EditStaffProfile) >= 0
        //                            || responsibilities.IndexOf(OCPConstant.StaffProfile.Responsibility.RemoveStaffProfile) >= 0)    //If user has Create/Edit/Remove Staff Profile responsibility,then display manage staff profile link
        //                        {
        //                            lbManageSP.Visible = true;
        //                        }
        //                        else
        //                        {
        //                            lbManageSP.Visible = false;
        //                        }

        //                        if (lbViewOS.Visible == false && lbViewOU.Visible == false && lbManageSP.Visible == false)
        //                        {
        //                            tbCP.Visible = false;
        //                        }
        //                        else
        //                            isCPWithRole = true;

        //                        if (responsibilities.IndexOf(OCPConstant.StaffProfile.Responsibility.CreateStaffProfile) >= 0)
        //                            hasCreateRes = true;

        //                        if (responsibilities.IndexOf(OCPConstant.StaffProfile.Responsibility.EditStaffProfile) >= 0)
        //                            hasEditRes = true;

        //                        if (webPartName == "ManageStaffProfile" && lbManageSP.CssClass == "leftmenuSelected")    //In View Broadcast page, when user has Create Broadcasts responsibility, then show the create button, else hide it
        //                        {
        //                            LinkButton lbCreate = (LinkButton)this.Parent.FindControl("lbCreate");
        //                            if (responsibilities.IndexOf(OCPConstant.StaffProfile.Responsibility.CreateStaffProfile) >= 0)
        //                                lbCreate.Visible = true;
        //                            else
        //                                lbCreate.Visible = false;
        //                        }

        //                        if (webPartName == "ViewStaffProfileByAdmin" && lbManageSP.CssClass == "leftmenuSelected")
        //                        {
        //                            LinkButton lbEdit = (LinkButton)this.Parent.FindControl("lbEdit");
        //                            Label lblSep = (Label)this.Parent.FindControl("lblSep");
        //                            if (responsibilities.IndexOf(OCPConstant.StaffProfile.Responsibility.EditStaffProfile) >= 0)
        //                                lbEdit.Visible = true;
        //                            else
        //                                lbEdit.Visible = false;

        //                            LinkButton lbRemove = (LinkButton)this.Parent.FindControl("lbRemove");
        //                            if (responsibilities.IndexOf(OCPConstant.StaffProfile.Responsibility.RemoveStaffProfile) >= 0)
        //                                lbRemove.Visible = true;
        //                            else
        //                                lbRemove.Visible = false;

        //                            if (lbEdit.Visible == true && lbRemove.Visible == true)
        //                                lblSep.Visible = true;
        //                            else
        //                                lblSep.Visible = false;
        //                        }
        //                    }
        //                }
        //                break;
        //            }
        //        }

        //    }

        //    string contextId;
        //    if (Request.QueryString["ContextId"] != null && Request.QueryString["ContextId"].ToString().Trim() != "")
        //        contextId = Request.QueryString["ContextId"].ToString();
        //    else
        //    {
        //        SPWeb CurrentWeb = SPContext.Current.Web;
        //        string _loginAccount = CurrentWeb.CurrentUser.LoginName.ToString();
        //        using (AccessFrameworkProxy accessProxy = new AccessFrameworkProxy())
        //        {
        //            AccessFrameworkDataset.StaffContextDataTable staffDT = accessProxy.GetStaffCtx(_loginAccount);
        //            if (staffDT.Rows.Count > 0)
        //                contextId = staffDT[0].OcpContextId.ToString();
        //            else
        //                contextId = "0";
        //        }
        //    }

        //    if (webPartName == "ManageStaffProfile" && isAppAdmin == false && isCPWithRole == false)
        //    {
        //        divMenu.Visible = false;
        //        HtmlTableCell tdLeftMenu = (HtmlTableCell)this.Parent.FindControl("tdLeftMenu");
        //        HtmlTableCell tdContent = (HtmlTableCell)this.Parent.FindControl("tdContent");
        //        tdLeftMenu.Visible = false;
        //        tdContent.Width = "100%";

        //        HiddenField hfNoMenu = (HiddenField)this.Parent.FindControl("hfNoMenu");
        //        hfNoMenu.Value = "Y";
        //    }
        //    else if (webPartName == "ViewStaffList")
        //    {
        //        if (Request.Params["hasm"] == null || (Request.Params["hasm"].Equals("vosp16a2bjkir1qr11562142f1s0r1hg33") == false && Request.Params["hasm"].Equals("voup16a2bjkir1qr11562142f1s0r1hg33") == false))
        //        {
        //            HtmlTableCell tdLeftMenu = (HtmlTableCell)this.Parent.FindControl("tdLeftMenu");
        //            HtmlTableCell tdContent = (HtmlTableCell)this.Parent.FindControl("tdContent");
        //            tdLeftMenu.Visible = false;
        //            tdContent.Width = "100%";

        //            HiddenField hfNoMenu = (HiddenField)this.Parent.FindControl("hfNoMenu");
        //            hfNoMenu.Value = "Y";
        //        }
        //    }
        //    //If user type in url ...mysite/mohpages/StaffProfile/CreateStaffProfile.aspx but not have create responsibility, then redirect to my site page
        //    else if (webPartName == "CreateStaffProfile")
        //    {
        //        if (isCPWithRole == false || hasCreateRes == false)
        //        {
        //            Response.Redirect("/MySite/default.aspx?ContextId=" + contextId);
        //        }
        //    }
        //    else if (webPartName == "EditStaffProfile")
        //    {
        //        if (isCPWithRole == false || hasEditRes == false)
        //        {
        //            Response.Redirect("/MySite/default.aspx?ContextId=" + contextId);
        //        }
        //    }
        //}
    }
}


